package com.happyhome.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.happyhome.constants.AppConstant;
import com.happyhome.constants.ComUnitName;
import com.happyhome.datamodel.Parameter;
import com.happyhome.datamodel.QParameter;
import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
public class ParameterDaoImpl extends GenericDAOImpl<Parameter, Long> implements ParameterDao{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory.getLogger(ParameterDaoImpl.class);
	
	@Override
	@PersistenceContext(unitName = ComUnitName.COM_UNIT)
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}

	@Override
	public List<Parameter> getDataConfigureForCavas() {
		JPQLQuery query = new JPAQuery(em());
		
		QParameter parameter = QParameter.parameter;
		
		BooleanBuilder where = new BooleanBuilder();

		where.and(parameter.codePara.eq(AppConstant.HOUSES_CODE))
			.or(parameter.codePara.eq(AppConstant.ROOMS_CODE))
			.or(parameter.codePara.eq(AppConstant.NEWS_CODE));
		
		query.from(parameter).where(where);
		
		List<Parameter> listResult = query.list(parameter);
		return listResult;
	}

	@Override
	public Parameter getParameterByCode(String code) {
		JPQLQuery query = new JPAQuery(em());
		
		QParameter parameter = QParameter.parameter;
		
		BooleanBuilder where = new BooleanBuilder();

		where.and(parameter.codePara.eq(code));
		
		query.from(parameter).where(where);
		
		Parameter result = query.uniqueResult(parameter);
		return result;
	}
	
}
